#include "StorageSettings.h"

#include <QString>

static const char * const groupKey = "EditorStorageSettings";
static const char * const keyCleanWhitespace = "CleanWhitespace";
static const char * const keyInEntireDocument = "InEntireDocument";
static const char * const keyAddFinalNewLine = "AddFinalNewLine";
static const char * const keyCleanIndentation = "CleanIndentation";

namespace CodeEdit {

StorageSettings::StorageSettings()
    : cleanWhitespace(true)
    , inEntireDocument(false)
    , cleanIndentation(true)
    , addFinalNewLine(true)
{
}

void StorageSettings::toSettings(QSettings *s) const
{
    s->beginGroup(QLatin1String(groupKey));
    s->setValue(QLatin1String(keyCleanWhitespace), cleanWhitespace);
    s->setValue(QLatin1String(keyInEntireDocument), inEntireDocument);
    s->setValue(QLatin1String(keyAddFinalNewLine), addFinalNewLine);
    s->setValue(QLatin1String(keyCleanIndentation), cleanIndentation);
    s->endGroup();
}

void StorageSettings::fromSettings(QSettings *s)
{
    s->beginGroup(QLatin1String(groupKey));
    cleanWhitespace = s->value(QLatin1String(keyCleanWhitespace), cleanWhitespace).toBool();
    inEntireDocument = s->value(QLatin1String(keyInEntireDocument), inEntireDocument).toBool();
    addFinalNewLine = s->value(QLatin1String(keyAddFinalNewLine), addFinalNewLine).toBool();
    cleanIndentation = s->value(QLatin1String(keyCleanIndentation), cleanIndentation).toBool();
    s->endGroup();
}

bool StorageSettings::equals(const StorageSettings &ts) const
{
    return addFinalNewLine == ts.addFinalNewLine
        && cleanWhitespace == ts.cleanWhitespace
        && inEntireDocument == ts.inEntireDocument
        && cleanIndentation == ts.cleanIndentation;
}

} // namespace CodeEdit
